package view;
import events.ResizeEvent;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import graphic.RessourceManager;
import gui.panels.ScrollPanel;
import view.events.LevelEditorViewOtherEvent;

/**
 * @author Damien Mabin
 */

class CreateRenderObjectPanel extends Sprite
{
	
	private var m_Style					: { backgroundColor : UInt, separatorColor : UInt, hightlightColor : UInt };
	private var m_ScrollStyle 			: { areaColor : UInt, barColor : UInt, buttonColor : UInt, barWidth : Float };
	
	private var m_Title					: TextField;
	private var m_ListRO				: ScrollPanel;
	private var m_AvailableObjectsList	: Array<{txtField : TextField, name : String, file : String}>;

	public function new(?_Style : { backgroundColor : UInt, separatorColor : UInt, hightlightColor : UInt }) 
	{
		super();
		
		m_AvailableObjectsList = new Array<{txtField : TextField, name : String, file : String}>();
		if (_Style == null)
		{
			m_Style = { backgroundColor : 0x000000, separatorColor : 0xFF0000, hightlightColor : 0xFFFF00 };
		}
		else
		{
			m_Style = _Style;
		}
		
		m_ScrollStyle = { areaColor : m_Style.backgroundColor, barColor : 0x000000, buttonColor : 0xFFED9C, barWidth : 15.0 };
		
		// =======================================================================
		// Title
		m_Title = new TextField();
		m_Title.text = "Available items :";
		m_Title.mouseEnabled = false;
		m_Title.selectable = false;
		
		// =======================================================================
		// Render Object list :
		m_ListRO = new ScrollPanel(100, 100, m_ScrollStyle);
		
		// =======================================================================
		// We add to the srcoll list all the elements available in the ressource manager :
		for (availableRO in RessourceManager.GetInstance().GetAvailableRenderObjects())
		{
			var txtField : TextField = new TextField();
			txtField.text = availableRO.name;
			txtField.selectable = false;
			//txtField.autoSize = TextFieldAutoSize.CENTER;
			txtField.height = 20;
			txtField.width = 300;
			txtField.backgroundColor = m_Style.hightlightColor;
			txtField.addEventListener(MouseEvent.MOUSE_DOWN, OnRenderObjectMouseEvent);
			m_ListRO.AddElement(txtField);
			m_AvailableObjectsList.push({txtField : txtField, name : availableRO.name, file : availableRO.file});
		}
		
		// =======================================================================
		
		this.addChild(m_Title);
		this.addChild(m_ListRO);
		
		this.addEventListener(ResizeEvent.EVENT, OnResizeEvent);
	}
	
	public function SetSelectedRenderObject(_Name:String, _File:String):Void
	{
		for (ro in m_AvailableObjectsList)
		{
			if (ro.name == _Name && ro.file == _File)
			{
				ro.txtField.background = true;
			}
			else
			{
				ro.txtField.background = false;
			}
		}
	}
	
	private function OnRenderObjectMouseEvent(_Evt:MouseEvent):Void
	{
		var selectedRenderObject : { txtField : TextField, name : String, file : String} = null;
		for (ro in m_AvailableObjectsList)
		{
			if (ro.txtField == _Evt.target)
			{
				if (!ro.txtField.background)
				{
					selectedRenderObject = ro;
				}
				break;
			}
		}
		
		#if debug
			if (selectedRenderObject != null)
				trace("User want to instanciate : " + selectedRenderObject.name);
			else
				trace("User want to instanciate : Que Dalle !!");
		#end
		var evt : LevelEditorViewOtherEvent;
		if (selectedRenderObject != null)
		{
			evt = new LevelEditorViewOtherEvent(E_OtherInteractionType.SELECT_RENDER_OBJECT_FOR_CREATION(selectedRenderObject.name, selectedRenderObject.file));
		}
		else
		{
			evt = new LevelEditorViewOtherEvent(E_OtherInteractionType.SELECT_RENDER_OBJECT_FOR_CREATION(null, null));
		}
		this.dispatchEvent(evt);
	}
	
	private function OnResizeEvent(_Evt:ResizeEvent):Void
	{
		// Draw background
		this.graphics.clear();
		this.graphics.beginFill(m_Style.backgroundColor);
		this.graphics.drawRect(0, 0, _Evt.m_Width, _Evt.m_Height);
		this.graphics.endFill();
		
		// Draw separator bar between title and scroll panel
		this.graphics.beginFill(m_Style.separatorColor);
		this.graphics.drawRect(5, 20, _Evt.m_Width-10, 2);
		this.graphics.endFill();
		
		// Place scroll panel :
		m_ListRO.x = 0;
		m_ListRO.y = 22;
		m_ListRO.dispatchEvent(new ResizeEvent(_Evt.m_Width, _Evt.m_Height-22));
	}
	
}